package com.gin.kotlin.no_gay_alliance.fragment.main.me

import android.view.View
import android.widget.CompoundButton
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.adapter.me.MissionAdapter
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.argument.MissionArgument
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingFragment
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.callback.RCallback
import com.gin.kotlin.no_gay_alliance.databinding.FragmentMainMeMissionBinding
import com.gin.kotlin.no_gay_alliance.model.MissionInfoModel
import com.gin.kotlin.no_gay_alliance.provider.ngaphp.MissionInfoProvider
import com.gin.nga.enums.param.MissionEvent
import com.gin.nga.enums.param.MissionType

class MissionFragment:AbstractBindingFragment<FragmentMainMeMissionBinding>(), RCallback<List<MissionInfoModel>, MissionArgument>, CompoundButton.OnCheckedChangeListener {
    private lateinit var adapter: MissionAdapter
    private var userId = 0L

    override fun onCreateView() {
        val b = this.b?:return
        this.adapter = MissionAdapter(requireContext(),handler!!)
        b.recyclerView.adapter = adapter
        b.swipeRefreshLayout.setOnRefreshListener { request(true) }

        // 自动签到开关
        b.swAutoMission.isChecked = App.INSTANCE.account.autoMission
        // 切换时更新
        b.swAutoMission.setOnCheckedChangeListener(this)
    }


    override fun onResume() {
        super.onResume()
        if (userId != App.INSTANCE.account.userId) {
            request()
            //  自动开关配置
            val b = this.b ?: return
            b.swAutoMission.setOnCheckedChangeListener(null)
            b.swAutoMission.isChecked = App.INSTANCE.account.autoMission
            b.swAutoMission.setOnCheckedChangeListener(this)
        }
    }


    private fun request(ignoreCache:Boolean=false){
        val b = this.b?:return
        b.tvWarning.visibility= View.GONE
        b.swipeRefreshLayout.isRefreshing= true

        this.userId = App.INSTANCE.account.userId
        MissionInfoProvider.getInstance().enqueue(MissionArgument(MissionEvent.SIGN_IN, MissionType.COUNTER),ignoreCache=ignoreCache, callback = this)
    }

    override fun onSuccess(data: List<MissionInfoModel>, ignoreCache: Boolean) {
        val b = this.b?:return
        b.swipeRefreshLayout.isRefreshing= false
        adapter.replaceData(data)

        if (data.isEmpty()){
            b.tvWarning.visibility= View.VISIBLE
            b.tvWarning.text = "暂无任务"
        }
    }

    override fun onError(errorMessage: String) {
        val b = this.b?:return
        b.swipeRefreshLayout.isRefreshing= false
        b.tvWarning.visibility= View.VISIBLE
        b.tvWarning.text = errorMessage
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        App.INSTANCE.account.autoMission = isChecked
        App.INSTANCE.database.accountDao().update(App.INSTANCE.account)
        if (isChecked) AlertDialogUtils.builder(requireContext(), message = getString(R.string.dailog_message_auto_mission)).show()
    }
}